package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.*;
import cn.gson.crm.model.mapper.CustomerMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.pojos.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class MergeService {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    CustomerDao customerDao;

    @Autowired
    SalesDao salesDao;

    @Autowired
    MergeDao mergeDao;

    @Autowired
    ContractDao contractDao;

    @Autowired
    FollowDao followDao;

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    LinkManDao linkManDao;

    @Autowired
    RefundRecordDao refundRecordDao;

    @Autowired
    DeliveryPlanDao deliveryPlanDao;

    @Autowired
    ExchangeGoodsDao exchangeGoodsDao;

    @Autowired
    MoneyBackPlanDao moneyBackPlanDao;

    @Autowired
    ReturngoodsDao returngoodsDao;

    @Autowired
    ComplaintsDao complaintsDao;

    @Autowired
    RepairOrderDao repairOrderDao;

    @Autowired
    CustomerServiceDao customerServiceDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    CompetitorDao competitorDao;

    @Autowired
    OfferDao offerDao;

    @Autowired
    SolutionDao solutionDao;

    //客户合并
    public List<String> updateMerge(int ygId,int customerId,int byCustomerId){
        Customer customer = customerDao.findById((long) byCustomerId).get();

        List<String> string = new ArrayList<>();

        //销售机会
        List<Sales> sales = customerMapper.timesSales(customerId);
        for (int i = 0; i < sales.size(); i++) {
            sales.get(i).setCustomer(customer);
            salesDao.save(sales.get(i));
        }
        string.add("销售机会：一共"+sales.size()+"条数据；合并");

        //合同
        List<Contract> contracts = customerMapper.timesContract(customerId,0);
        for (int i = 0; i < contracts.size(); i++) {
            contracts.get(i).setCustomerId(customer);
            contractDao.save(contracts.get(i));
        }
        string.add("合同：一共"+contracts.size()+"条数据；合并");

        //跟进记录
        List<Follow> follows = customerMapper.timesFollow(customerId,0);
        for (int i = 0; i < follows.size(); i++) {
            follows.get(i).setCustomerByCustomerId(customer);
            followDao.save(follows.get(i));
        }
        string.add("跟进记录：一共"+follows.size()+"条数据；合并");

        //联系人
        List<Linkman> linkmen = customerMapper.mergeLinkman(customerId);
        for (int i = 0; i < linkmen.size(); i++) {
            linkmen.get(i).setCustomerByCustomerId(customer);
            linkManDao.save(linkmen.get(i));
        }
        string.add("联系人：一共"+linkmen.size()+"条数据；合并");

        //回款记录
        List<RefundRecord> refund = customerMapper.timesRefund(customerId,0);
        for (int i = 0; i < refund.size(); i++) {
            refund.get(i).setCustomerByCustomerId(customer);
            refundRecordDao.save(refund.get(i));
        }
        string.add("回款记录：一共"+refund.size()+"条数据；合并");

        //交付计划
        List<DeliveryPlan> plan = customerMapper.timesPlan(customerId,0);
        for (int i = 0; i < plan.size(); i++) {
            plan.get(i).setCustomerByCustomerId(customer);
            deliveryPlanDao.save(plan.get(i));
        }
        string.add("交付计划：一共"+plan.size()+"条数据；合并");

        //换货
        List<ExchangeGoods> exchange = customerMapper.timesExchange(customerId,0);
        for (int i = 0; i < exchange.size(); i++) {
            exchange.get(i).setCustomerByCustomerId(customer);
            exchangeGoodsDao.save(exchange.get(i));
        }
        string.add("换货：一共"+exchange.size()+"条数据；合并");

        //回款计划
        List<MoneyBackPlan> money = customerMapper.timesMoney(customerId,0);
        for (int i = 0; i < money.size(); i++) {
            money.get(i).setCustomerByCustomerId(customer);
            moneyBackPlanDao.save(money.get(i));
        }
        string.add("回款计划：一共"+money.size()+"条数据；合并");

        //退货
        List<ReturnGoods> returnGoods = customerMapper.timesReturn(customerId,0);
        for (int i = 0; i < returnGoods.size(); i++) {
            returnGoods.get(i).setCustomerByCustomerId(customer);
            returngoodsDao.save(returnGoods.get(i));
        }
        string.add("退货：一共"+returnGoods.size()+"条数据；合并");

        //维修工单repair_order
        List<RepairOrder> repairOrders = customerMapper.timesRepair(customerId,0);
        for (int i = 0; i < repairOrders.size(); i++) {
            repairOrders.get(i).setCustomerByCustomerId(customer);
            repairOrderDao.save(repairOrders.get(i));
        }
        string.add("维修工单：一共"+repairOrders.size()+"条数据；合并");


        //客户投诉complaints
        List<Complaints> complaints = customerMapper.timesComplaints(customerId,0);
        for (int i = 0; i < complaints.size(); i++) {
            complaints.get(i).setCustomerByCustomerId(customer);
            complaintsDao.save(complaints.get(i));
        }
        string.add("客户投诉：一共"+complaints.size()+"条数据；合并");

        //客户服务customer_service
        List<CustomerService> customerServices = customerMapper.timesService(customerId,0);
        for (int i = 0; i < customerServices.size(); i++) {
            customerServices.get(i).setCustomerByCustomerId(customer);
            customerServiceDao.save(customerServices.get(i));
        }
        string.add("客户服务：一共"+customerServices.size()+"条数据；合并");

        Merge merge = new Merge();
        merge.setMergeDate(new Date());
        Customer customer1 = customerDao.findById((long) customerId).get();
        Customer customer2 = customerDao.findById((long) byCustomerId).get();
        merge.setCustomer(customer1);
        merge.setCustomered(customer2);
        Employee employee = employeeDao.findById((long)ygId).get();
        merge.setEmployeeByYgId(employee);
        String detail = "将客户：（"+customer1.getCustomerName()+"）合并到客户：（"+customer2.getCustomerName()+"）";
        merge.setMergeDetail(detail);
        mergeDao.save(merge);
        return string;
    }

    //客户合并数据
    public List<Merge> mergeSelect(){
        List<Merge> merges = customerMapper.mergeSelect();
        return customerMapper.mergeSelect();
    }

    //时间线：销售机会查询
    public List<Sales> timeSales(int salesId){
//        Sales sales = salesDao.findById((long) salesId).get();
        List<Sales> sales = customerMapper.detailSales(salesId);
        return sales;
    }

    //时间线：竞争对手
    public Competitor timeCompetitor(int comId){
        return competitorDao.findById((long)comId).get();
    }

    //时间线：报价
    public Offer timeOffer(int offerId){
        return offerDao.findById((long)offerId).get();
    }

    //时间线：解决方案
    public Solution timeSolution(int solutionId){
        return solutionDao.findById((long)solutionId).get();
    }

    //时间线：跟进记录
    public Follow timeFollow(int followId){
        return followDao.findById((long) followId).get();
    }

    //时间线：合同
    public Contract timeContract(int contractId){
        return contractDao.findById(contractId).get();
    }

    //时间线：订单
    public Order timeOrder(int orderId){
        return orderDao.findById(orderId).get();
    }

    //时间线：回款记录
    public RefundRecord timeRefund(int refundId){
        return refundRecordDao.findById(refundId).get();
    }

    //回款计划
    public MoneyBackPlan timeMoney(int moneyId){
        return moneyBackPlanDao.findById(moneyId).get();
    }

    //时间线：退货
    public ReturnGoods timeReturn(int returnId){
        return returngoodsDao.findById(returnId).get();
    }

    //时间线：换货
    public ExchangeGoods timeExc(int excId){
        return exchangeGoodsDao.findById(excId).get();
    }

    //时间线：交付计划
    public DeliveryPlan timeDelivery(int deliveryId){
        return deliveryPlanDao.findById(deliveryId).get();
    }

    //时间线：维修工单
    public RepairOrder timeRepair(int repairId){
        return repairOrderDao.findById(repairId).get();
    }

    //时间线：客户投诉
    public Complaints timeComplaints(int complaintsId){
        return complaintsDao.findById((long)complaintsId).get();
    }

    //时间线：客户服务
    public CustomerService timeService(int serviceId){
        return customerServiceDao.findById((long)serviceId).get();
    }
}
